#! /usr/bin/python3
import requests
from lxml import etree
import pandas as pd
import operator
import time
import random
import ast
import json

wan_ = {}
qian_ = {}
bai_ = {}
shi_ = {}

def show_data(flag, list_01):
    code_count = {'0': 0, '1': 0, '2': 0, '3': 0, '4': 0, \
                  '5': 0, '6': 0, '7': 0, '8': 0, '9': 0}
    code = ''
    count = ''
    for i in list_01:
        if i == ' ':
            continue
        else:
            code_count[str(i)] = code_count[str(i)] + 1

    for key in code_count:
        code = code + "  " + str(key)
        count = count + "  " + str(code_count[key])

    #print(f"--[ {flag} ]----[ {10} ]--")
    #print(code)
    #print(count)

    if 'wan' in flag:
        wan_[flag] = count

    if 'qian' in flag:
        qian_[flag] = count

    if 'bai' in flag:
        bai_[flag] = count

    if 'shi' in flag:
        shi_[flag] = count

def cai_bao_bei():
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36', \
                 'cookie' : 'LastUrl=; __utma=56961525.1081056271.1641197659.1641197659.1641197659.1; __utmc=56961525; __utmz=56961525.1641197659.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lvt_213d524a1d07274f17dfa17b79db318f=1641197659; FirstOKURL=https%3A//www.okooo.com/p5/p5sh/; First_Source=www.okooo.com; PHPSESSID=359808b3009a3a0cb3d3b933418d4e6697f205aa; pm=; LStatus=N; LoginStr=%7B%22welcome%22%3A%22%u60A8%u597D%uFF0C%u6B22%u8FCE%u60A8%22%2C%22login%22%3A%22%u767B%u5F55%22%2C%22register%22%3A%22%u6CE8%u518C%22%2C%22TrustLoginArr%22%3A%7B%22alipay%22%3A%7B%22LoginCn%22%3A%22%u652F%u4ED8%u5B9D%22%7D%2C%22tenpay%22%3A%7B%22LoginCn%22%3A%22%u8D22%u4ED8%u901A%22%7D%2C%22weibo%22%3A%7B%22LoginCn%22%3A%22%u65B0%u6D6A%u5FAE%u535A%22%7D%2C%22renren%22%3A%7B%22LoginCn%22%3A%22%u4EBA%u4EBA%u7F51%22%7D%2C%22baidu%22%3A%7B%22LoginCn%22%3A%22%u767E%u5EA6%22%7D%2C%22snda%22%3A%7B%22LoginCn%22%3A%22%u76DB%u5927%u767B%u5F55%22%7D%7D%2C%22userlevel%22%3A%22%22%2C%22flog%22%3A%22hidden%22%2C%22UserInfo%22%3A%22%22%2C%22loginSession%22%3A%22___GlobalSession%22%7D; Hm_lpvt_213d524a1d07274f17dfa17b79db318f=1641199582; acw_tc=2f624a7d16412016154231285e223a2e58d6c7528af125a8274a7e7527075d'
                 }
    url_dict = {'cai_sha_p3_wan':'https://expert.78500.cn/p3/dxbw/' , \
                'cai_sha_p3_qian':'https://expert.78500.cn/p3/dxsw/' , \
                'cai_sha_p3_bai':'https://expert.78500.cn/p3/dxsw/'
                }
    for key in url_dict:
        interval = random.randint(2, 4)
        time.sleep(interval)
        #print(url_dict[key])
        r1 = requests.get(url_dict[key], headers=headers)
        parse_html = etree.HTML(r1.text)

        # /html/body/div/table/tbody/tr/td/text()
        s_yu_list = parse_html.xpath('/html/body/div/table/tbody/tr/td/text()')
        #print(s_yu_list)
        tmp_list = []
        for j in range(len(s_yu_list)-1):
            #print(s_yu_list[j])
            if '当前期杀号' in s_yu_list[j] :
                for k in range(j+1,j+20,2):
                    #print(s_yu_list[k])
                    tmp_list.append(s_yu_list[k])
        #print(tmp_list)
        show_data(key, tmp_list)


def oook():
    headers01 = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36', \
                 'cookie' : 'LastUrl=; __utma=56961525.1081056271.1641197659.1641197659.1641197659.1; __utmc=56961525; __utmz=56961525.1641197659.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lvt_213d524a1d07274f17dfa17b79db318f=1641197659; FirstOKURL=https%3A//www.okooo.com/p5/p5sh/; First_Source=www.okooo.com; PHPSESSID=359808b3009a3a0cb3d3b933418d4e6697f205aa; pm=; LStatus=N; LoginStr=%7B%22welcome%22%3A%22%u60A8%u597D%uFF0C%u6B22%u8FCE%u60A8%22%2C%22login%22%3A%22%u767B%u5F55%22%2C%22register%22%3A%22%u6CE8%u518C%22%2C%22TrustLoginArr%22%3A%7B%22alipay%22%3A%7B%22LoginCn%22%3A%22%u652F%u4ED8%u5B9D%22%7D%2C%22tenpay%22%3A%7B%22LoginCn%22%3A%22%u8D22%u4ED8%u901A%22%7D%2C%22weibo%22%3A%7B%22LoginCn%22%3A%22%u65B0%u6D6A%u5FAE%u535A%22%7D%2C%22renren%22%3A%7B%22LoginCn%22%3A%22%u4EBA%u4EBA%u7F51%22%7D%2C%22baidu%22%3A%7B%22LoginCn%22%3A%22%u767E%u5EA6%22%7D%2C%22snda%22%3A%7B%22LoginCn%22%3A%22%u76DB%u5927%u767B%u5F55%22%7D%7D%2C%22userlevel%22%3A%22%22%2C%22flog%22%3A%22hidden%22%2C%22UserInfo%22%3A%22%22%2C%22loginSession%22%3A%22___GlobalSession%22%7D; Hm_lpvt_213d524a1d07274f17dfa17b79db318f=1641199582; acw_tc=2f624a7d16412016154231285e223a2e58d6c7528af125a8274a7e7527075d'
                 }

    url_dict = {'oook_sha_p5_wan':'https://www.okooo.com/ajax/shahao/P5/20/N1/', \
                'oook_sha_p5_qian':'https://www.okooo.com/ajax/shahao/P5/20/N2/', \
                'oook_sha_p5_bai':'https://www.okooo.com/ajax/shahao/P5/20/N3/', \
                'oook_sha_p5_shi':'https://www.okooo.com/ajax/shahao/P5/20/N4/', \
                'oook_sha_p3_wan':'https://www.okooo.com/ajax/shahao/P3/20/N1/', \
                'oook_sha_p3_qian':'https://www.okooo.com/ajax/shahao/P3/20/N2/', \
                'oook_sha_p3_bai':'https://www.okooo.com/ajax/shahao/P3/20/N3/'
                }

    for key in url_dict:
        tmp_list = []
        interval = random.randint(2,4)
        time.sleep(interval)
        #print(url_dict[key])
        r1 = requests.get(url_dict[key], headers=headers01)
        str_text = r1.text
        #print(str_text)
        text_json = json.loads(str_text)
        for key01 in text_json:
            # print(text_json[key])
            if text_json[key01]['LotteryResult'] == '':
                kill_number = text_json[key01]['KillNum']
                #print(kill_number)
                for k01 in kill_number:
                    tmp_list.append(kill_number[k01]['Num'])
        #print(tmp_list)
        show_data(key, tmp_list)


def cai_jing_wang():
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36', \
                 'cookie' : 'LastUrl=; __utma=56961525.1081056271.1641197659.1641197659.1641197659.1; __utmc=56961525; __utmz=56961525.1641197659.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lvt_213d524a1d07274f17dfa17b79db318f=1641197659; FirstOKURL=https%3A//www.okooo.com/p5/p5sh/; First_Source=www.okooo.com; PHPSESSID=359808b3009a3a0cb3d3b933418d4e6697f205aa; pm=; LStatus=N; LoginStr=%7B%22welcome%22%3A%22%u60A8%u597D%uFF0C%u6B22%u8FCE%u60A8%22%2C%22login%22%3A%22%u767B%u5F55%22%2C%22register%22%3A%22%u6CE8%u518C%22%2C%22TrustLoginArr%22%3A%7B%22alipay%22%3A%7B%22LoginCn%22%3A%22%u652F%u4ED8%u5B9D%22%7D%2C%22tenpay%22%3A%7B%22LoginCn%22%3A%22%u8D22%u4ED8%u901A%22%7D%2C%22weibo%22%3A%7B%22LoginCn%22%3A%22%u65B0%u6D6A%u5FAE%u535A%22%7D%2C%22renren%22%3A%7B%22LoginCn%22%3A%22%u4EBA%u4EBA%u7F51%22%7D%2C%22baidu%22%3A%7B%22LoginCn%22%3A%22%u767E%u5EA6%22%7D%2C%22snda%22%3A%7B%22LoginCn%22%3A%22%u76DB%u5927%u767B%u5F55%22%7D%7D%2C%22userlevel%22%3A%22%22%2C%22flog%22%3A%22hidden%22%2C%22UserInfo%22%3A%22%22%2C%22loginSession%22%3A%22___GlobalSession%22%7D; Hm_lpvt_213d524a1d07274f17dfa17b79db318f=1641199582; acw_tc=2f624a7d16412016154231285e223a2e58d6c7528af125a8274a7e7527075d'
                 }
    url_dict = {'cj_sha_p5_wan': 'https://zst.cjcp.com.cn/shdd/0-szs-pl5-diyiwei.html', \
                'cj_sha_p5_qian': 'https://zst.cjcp.com.cn/shdd/0-szs-pl5-dierwei.html', \
                'cj_sha_p5_bai': 'https://zst.cjcp.com.cn/shdd/0-szs-pl5-disanwei.html', \
                'cj_sha_p5_shi': 'https://zst.cjcp.com.cn/shdd/0-szs-pl5-disiwei.html'

                }
    for key in url_dict:
        tmp_list = []
        interval = random.randint(2,4)
        time.sleep(interval)
        #print(url_dict[key])
        r1 = requests.get(url_dict[key], headers=headers)
        parse_html = etree.HTML(r1.text)
        #print(r1.text)
        #/html/body/div/div/div/table/tbody/tr/td/text()
        s_yu_list = parse_html.xpath('/html/body/div/div/div/table/tr/td/text()')
        #print(s_yu_list)
        for j in range(len(s_yu_list)-1):
            if '当前期杀号' in s_yu_list[j]:
                for k in range(j+1, j+11):
                    tmp_list.append(s_yu_list[k])
        show_data(key, tmp_list)
        #print(tmp_list)

def show_all_data():
    theme = "  0  1  2  3  4  5  6  7  8  9"

    print("-------------------------万位杀码-------------------")
    print(theme)
    for key in wan_:
        print(wan_[key])

    print("-------------------------千位杀码-------------------")
    print(theme)
    for key in qian_:
        print(qian_[key])

    print("-------------------------百位杀码-------------------")
    print(theme)
    for key in bai_:
        print(bai_[key])

    print("-------------------------十位杀码-------------------")
    print(theme)
    for key in shi_:
        print(shi_[key])

if __name__ == "__main__":
    print("start............")
    #01 彩宝贝杀
    cai_bao_bei()

    #02 oook杀
    oook()

    #03 财经杀
    cai_jing_wang()


    #04 展示数据
    show_all_data()
    print(".......")
